
import { defineStore } from 'pinia'

export const useSearchStore = defineStore('search', {
  state: () => ({
    history: []
  }),
  actions: {
    addHistory(term) {
      if (this.history.includes(term)) {
        this.history = this.history.filter(item => item !== term);
      }
      this.history.unshift(term);
      if (this.history.length > 10) {
        this.history.pop();
      }
    },
    clearHistory() {
      this.history = [];
    }
  }
},
 {
    // 网页端配置
    // persist: true,
    // 小程序端配置
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },);